﻿import { Component, Injector, ViewChild} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { Tab } from './tab.model';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { AuditAuthorityEditComponent } from './audit-authority_edit.component'
import { RequirementPackageServiceProxy,RequirementPackageState,RoleUserServiceProxy,CheckerRoleEnum,RoleUserDto,DepartmentServiceProxy } from '@shared/service-proxies/service-proxies';
import { AuditAuthorityTabComponent } from './audit-authority_tab.component';
import { AbpSessionService } from '@abp/session/abp-session.service';

@Component({
    templateUrl: './audit-authority.component.html',
    styleUrls: ['./audit-authority.component.less'],
    animations: [appModuleAnimation()]
})
export class AuditAuthorityComponent extends AppComponentBase{
    @ViewChild('auditAuthorityEditComponent') auditAuthorityEditComponent: AuditAuthorityEditComponent;
    @ViewChild('auditAuthorityTabComponent') auditAuthorityTabComponent: AuditAuthorityTabComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    tabs: Array<Tab>;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;
    selectAll: boolean = true;
    unitOfUsings: Map<String,String>;

    constructor(
        injector: Injector,
        private _requirementPackageService: RequirementPackageServiceProxy,
        private _roleUserServiceProxy:RoleUserServiceProxy,
        private _departmentServiceProxy:DepartmentServiceProxy,
        private _abpSessionService :AbpSessionService) {
        super(injector);
        this.tabs = new Array<Tab>();
        this.getAllUnitOfUsings();
        this.addTab();
    }
    getAllUnitOfUsings() {
        this.unitOfUsings = new Map<String, String>()
        this._departmentServiceProxy.getAllDpts()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                if (result != null && result.length > 0) {
                    for (var i = 0; i < result.length; i++) {
                        this.unitOfUsings[result[i].value] = result[i].name;
                    }
                }
            });
    }
    getUnitOfUsingsName(value: string) {
        var name = this.unitOfUsings[value];
        if (name) {
            return name;
        } else {
            return value;
        }
    }
    addTab(): void {
        let tab: Tab = new Tab("生产处审批人");
        tab.index = 1;
        this.tabs.push(tab);

        let tab1: Tab = new Tab("出库管理员");
        tab1.index = 2;
        this.tabs.push(tab1);

        let tab2: Tab = new Tab("质检人员");
        tab2.index = 3;
        this.tabs.push(tab2);
    }
    clearTab(): void {

    }

    /**
    * 弹出添加需求详细页面
    * 弹出编辑需求详细页面
    * @param requirementItemDto 
    */
    createOrEditRequirementItem(_pRoleUserDto : RoleUserDto):void{
        this.auditAuthorityEditComponent.show(_pRoleUserDto);
    }
    createOrEditRequirementPackage(): void {
        this.auditAuthorityEditComponent.show(null);
    }
    deleteRequirementPackage(roleUserDto :RoleUserDto): void {
        this.message.confirm(
            '该记录将被删除',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._roleUserServiceProxy.deleteUserOfRole(roleUserDto.id)
                    .pipe(finalize(() => { }))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                        this.getRequirementPackages()
                    });
                }
            }
        );
    }
    getRequirementPackages(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._roleUserServiceProxy.getUsersByRole(0)//CheckerRoleEnum.车间审批领导
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.length;
                this.primengTableHelper.records = result;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    getRoleName(code): string {
        return CheckerRoleEnum[code];
    }

    getRequirementPackageStateString(state:number):string{
        return RequirementPackageState[state];
    }

    downloadWebLogs = function () {
        const self = this;
        self._webLogService.downloadWebLogs().subscribe((result) => {
            self._fileDownloadService.downloadTempFile(result);
        });
    };

    onResize(event): void {

    }

    ngOnInit(): void {

    }
}
